<?php

namespace App\Http\Controllers\Admin;

use App\Models\BookHomeReadingList;
use App\Validate\BookHomeReadingListValidate;
use Illuminate\Support\Facades\DB;

/**
 * 图书到家阅读书单表（图书馆端）
 * Class Banner
 * @package app\api\controller
 */
class BookHomeReadingListLibController extends CommonController
{
    public $model;
    public $validate = null;
    public $type = 2;


    public function __construct()
    {
        parent::__construct();

        $this->model = new BookHomeReadingList();
        $this->validate = new BookHomeReadingListValidate();
    }


    /**
     * 筛选(用于下拉框选择)
    // * @param type 类型 1 新书 2馆藏书
     */
    public function filterList()
    {
        $lib_all_id = $this->getAdminShopIdAll();
        if (empty($lib_all_id)) {
            return $this->returnApi(203, '暂无数据'); //无权限访问
        }

        $condition[] = ['is_del', '=', 1];
        $condition[] = ['type', '=', $this->type];

        return $this->model->getFilterList(['id', 'name', 'intro', 'type'], $condition);
    }


    /**
     * 列表
     * @param page int 页码
     * @param limit int 分页大小
   //  * @param type    类型 1 新书 2馆藏书
     * @param keywords string 搜索关键词
     */
    public function lists()
    {
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? $this->request->limit : 10;

        $this->request->merge(['type' => $this->type]); //固定查看书店的数据

        $res = $this->model->lists($this->request->all());
        if (empty($res['data'])) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);

        return $this->returnApi(200, "查询成功", true, $res);
    }


    /**
     * 详情
     * @param id int
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->detail($this->request->id);

        if (empty($res)) {
            return $this->returnApi(203, "暂无数据");
        }

        return $this->returnApi(200, "查询成功", true, $res);
    }

    /**
     * 新增
     * @param name string 名称
     * @param intro string 简介
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('lib_add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $condition[] = ['type', '=', $this->type];
        $is_exists = $this->model->nameIsExists($this->request->name, 'name', null, 1, $condition);

        if ($is_exists) {
            return $this->returnApi(202, "此名称已存在");
        }
        $this->request->merge(['type' => $this->type]); //固定查看书店的数据
        $this->request->merge(['manage_id' => $this->request->manage_id]); //固定查看书店的数据

        $res = $this->model->add($this->request->all());

        if (!$res) {
            return $this->returnApi(202, "新增失败");
        }
        return $this->returnApi(200, "新增成功", true);
    }


    /**
     * 修改
     * @param id int banner id
     * @param name string 名称
     * @param intro string 简介
     */
    public function change()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('lib_change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $condition[] = ['type', '=', $this->type];
        $is_exists = $this->model->nameIsExists($this->request->name, 'name', $this->request->id, 1, $condition);

        if ($is_exists) {
            return $this->returnApi(202, "此名称已存在");
        }
        $this->request->merge(['type' => $this->type]); //固定查看书店的数据
        $res = $this->model->change($this->request->all());

        if (!$res) {
            return $this->returnApi(202, "修改失败");
        }
        return $this->returnApi(200, "修改成功", true);
    }

    /**
     * 删除
     * @param id int
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->del($this->request->id);
        if ($res === true) {
            return $this->returnApi(200, "删除成功", true);
        }
        return $this->returnApi(202, $res);
    }
}
